//
//  PositionReportingManager.m
//  Tazama NGO
//
//  Created by Jack Sheriff on 10/17/11.
//  Copyright (c) 2011 The MITRE Corporation. All rights reserved.
//

#import <CoreLocation/CoreLocation.h>
#import "PositionReportManager.h"
#import "Settings.h"

@interface PositionReportManager ()

@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) NSTimer *timer;

@end

@implementation PositionReportManager

@synthesize locationManager = iv_locationManager;
@synthesize timer = iv_timer;

static PositionReportManager *sharedInstance = nil;

+ (PositionReportManager *)sharedInstance
{
    if (sharedInstance == nil) {
        sharedInstance = [[super allocWithZone:NULL] init];
    }
    return sharedInstance;
}

+ (id)allocWithZone:(NSZone *)zone
{
    return [[self sharedInstance] retain];
}

- (id)init
{
    if ((self = [super init]))
	{
		
    }
    return self;
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain
{
    return self;
}

- (NSUInteger)retainCount
{
    return NSUIntegerMax;  //denotes an object that cannot be released
}

- (oneway void)release
{
    //do nothing
}

- (id)autorelease
{
    return self;
}

-(void)startReporting {
    // Get the update period
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    int updatePeriodKey = [[defaults valueForKey:kKmlFeedUpdatePeriod] intValue];
//    double updatePeriod = 120.0;
//    if (updatePeriodKey == 0) {
//        updatePeriod = 30.0;
//    }
//    else if (updatePeriodKey == 1) {
//        updatePeriod = 60.0;
//    }
//    else if (updatePeriodKey == 2) {
//        updatePeriod = 120.0;
//    }
    
    if (self.locationManager == nil)
        self.locationManager = [[CLLocationManager alloc] init];
    
//    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    
    // Set a movement threshold for new events.
    self.locationManager.distanceFilter = 500;
    
    [self.locationManager startUpdatingLocation];
    
    NSTimer *newTimer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(sendReport:) userInfo:nil repeats:YES];
    self.timer = newTimer;
}

-(void)sendReport:(NSTimer *)timer
{
    CLLocation *location = self.locationManager.location;
    if (location) {
        
    }
}

@end
